package zmee.pmpt.formplugin;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.metadata.IDataEntityProperty;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.EventObject;

/**
 * @author yss
 * @createTime 2022-12-29 10:21
 * @description 主项计划编制扩展插件
 */
public class MasterPlanBillPlugin extends AbstractBillPlugIn {

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        setProleader((DynamicObject) getModel().getValue("project"));
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        IDataEntityProperty property = e.getProperty();
        ChangeData changeData = e.getChangeSet()[0];
        Object newValue = changeData.getNewValue();
        Object oldValue = changeData.getOldValue();
        String name = property.getName();
        if("project".equals(name)){
            setProleader((DynamicObject) newValue);
        }
    }

    public void setProleader(DynamicObject projectDy){
        Long proerId = 0L;
        if(projectDy != null){
            DynamicObject appDy = BusinessDataServiceHelper.loadSingleFromCache("pmas_pro_approval", "proleader",
                    new QFilter[]{new QFilter("pro.id", QCP.equals, projectDy.getLong("id"))});
            if(appDy != null){
                DynamicObject proleaderDy = appDy.getDynamicObject("proleader");
                if(proleaderDy != null){
                    proerId = proleaderDy.getLong("id");
                }
            }
        }
        this.getModel().setValue("zmee_proleader",proerId);
    }
}
